﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Devices.Sensors;

namespace Experiments.MichaelKappel.Net.Tilt
{
    public sealed partial class SimplePage : PhoneApplicationPage, IDisposable
    {
        Accelerometer accelerometer = new Accelerometer();
        public SimplePage()
        {
            InitializeComponent();
            accelerometer.Start();
            accelerometer.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs>(accelerometer_ReadingChanged);
        }

        private void accelerometer_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() => updateScreen(e));
        }
        private void updateScreen(AccelerometerReadingEventArgs e)
        {
            Double x, y, z;

            if (ddlX.SelectedIndex == 0)
                x = e.X * 90;
            else if (ddlX.SelectedIndex == 1)
                x = e.Y * 90;
            else
                x = e.Z * 90;

            if (ddlY.SelectedIndex == 0)
                y = e.X * 90;
            else if (ddlY.SelectedIndex == 1)
                y = e.Y * 90;
            else
                y = e.Z * 90;

            if (ddlZ.SelectedIndex == 0)
                z = e.X * 90;
            else if (ddlZ.SelectedIndex == 1)
                z = e.Y * 90;
            else
                z = e.Z * 90;

            ppSymbolX.RotationX = (x + (sX.Value * 45)) * ((!cbxX.IsChecked.Value) ? 1 : -1);
            ppSymbolX.RotationY = y * ((!cbxX.IsChecked.Value) ? 1 : -1);
            ppSymbolX.RotationZ = z * ((!cbxX.IsChecked.Value) ? 1 : -1);

            ppSymbolY.RotationX = x * ((!cbxY.IsChecked.Value) ? 1 : -1);
            ppSymbolY.RotationY = (y + (sY.Value * 45)) * ((!cbxY.IsChecked.Value) ? 1 : -1);
            ppSymbolY.RotationZ = z * ((!cbxY.IsChecked.Value) ? 1 : -1);

            ppSymbolZ.RotationX = x * ((!cbxZ.IsChecked.Value) ? 1 : -1);
            ppSymbolZ.RotationY = y * ((!cbxZ.IsChecked.Value) ? 1 : -1);
            ppSymbolZ.RotationZ = (z + (sZ.Value * 45)) * ((!cbxZ.IsChecked.Value) ? 1 : -1);

            txtX.Text = e.X.ToString("0.00");
            txtY.Text = e.Y.ToString("0.00");
            txtZ.Text = e.Z.ToString("0.00");
        }

        public void Dispose()
        {
            accelerometer.Dispose();
        }

        private void cbxXVisible_Click(object sender, RoutedEventArgs e)
        {
            imgSymbolX.Visibility = (!cbxXVisible.IsChecked.HasValue || cbxXVisible.IsChecked.Value) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
        }
        private void cbxYVisible_Click(object sender, RoutedEventArgs e)
        {
            imgSymbolY.Visibility = (!cbxYVisible.IsChecked.HasValue || cbxYVisible.IsChecked.Value) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
        }
        private void cbxZVisible_Click(object sender, RoutedEventArgs e)
        {
            imgSymbolZ.Visibility = (!cbxZVisible.IsChecked.HasValue || cbxZVisible.IsChecked.Value) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
        }
   
    }
}